/**
 * @file count_last_flip_bmi2.c
 *
 *
 * A function is provided to count the number of fipped disc of the last move.
 *
 * The basic principle is to read into an array a precomputed result. Doing
 * this is easy for a single line ; as we can use arrays of the form:
 *  - COUNT_FLIP[square where we play][8-bits disc pattern].
 * The problem is thus to convert any line of a 64-bits disc pattern into an
 * 8-bits disc pattern. A fast way to do this is to select the right line,
 * with a bit-mask, to gather the masked-bits into a continuous set by the 
 * BMI2 PEXT instruction.
 * Once we get our 8-bits disc patterns, we directly get the number of
 * flipped discs from the precomputed array, and add them from each flipping
 * lines.
 * For optimization purpose, the value returned is twice the number of flipped
 * disc, to facilitate the computation of disc difference.
 *
 * @date 1998 - 2014
 * @author Richard Delorme
 * @author Toshihiko Okuhara
 * @version 4.4
 * 
 */

#include <x86intrin.h>

/** precomputed count flip array */
static const unsigned char COUNT_FLIP[8][256] = {
	{
		 0,  0,  0,  0,  2,  2,  0,  0,  4,  4,  0,  0,  2,  2,  0,  0,  6,  6,  0,  0,  2,  2,  0,  0,  4,  4,  0,  0,  2,  2,  0,  0,
		 8,  8,  0,  0,  2,  2,  0,  0,  4,  4,  0,  0,  2,  2,  0,  0,  6,  6,  0,  0,  2,  2,  0,  0,  4,  4,  0,  0,  2,  2,  0,  0,
		10, 10,  0,  0,  2,  2,  0,  0,  4,  4,  0,  0,  2,  2,  0,  0,  6,  6,  0,  0,  2,  2,  0,  0,  4,  4,  0,  0,  2,  2,  0,  0,
		 8,  8,  0,  0,  2,  2,  0,  0,  4,  4,  0,  0,  2,  2,  0,  0,  6,  6,  0,  0,  2,  2,  0,  0,  4,  4,  0,  0,  2,  2,  0,  0,
		12, 12,  0,  0,  2,  2,  0,  0,  4,  4,  0,  0,  2,  2,  0,  0,  6,  6,  0,  0,  2,  2,  0,  0,  4,  4,  0,  0,  2,  2,  0,  0,
		 8,  8,  0,  0,  2,  2,  0,  0,  4,  4,  0,  0,  2,  2,  0,  0,  6,  6,  0,  0,  2,  2,  0,  0,  4,  4,  0,  0,  2,  2,  0,  0,
		10, 10,  0,  0,  2,  2,  0,  0,  4,  4,  0,  0,  2,  2,  0,  0,  6,  6,  0,  0,  2,  2,  0,  0,  4,  4,  0,  0,  2,  2,  0,  0,
		 8,  8,  0,  0,  2,  2,  0,  0,  4,  4,  0,  0,  2,  2,  0,  0,  6,  6,  0,  0,  2,  2,  0,  0,  4,  4,  0,  0,  2,  2,  0,  0,
	},
	{
		 0,  0,  0,  0,  0,  0,  0,  0,  2,  2,  2,  2,  0,  0,  0,  0,  4,  4,  4,  4,  0,  0,  0,  0,  2,  2,  2,  2,  0,  0,  0,  0,
		 6,  6,  6,  6,  0,  0,  0,  0,  2,  2,  2,  2,  0,  0,  0,  0,  4,  4,  4,  4,  0,  0,  0,  0,  2,  2,  2,  2,  0,  0,  0,  0,
		 8,  8,  8,  8,  0,  0,  0,  0,  2,  2,  2,  2,  0,  0,  0,  0,  4,  4,  4,  4,  0,  0,  0,  0,  2,  2,  2,  2,  0,  0,  0,  0,
		 6,  6,  6,  6,  0,  0,  0,  0,  2,  2,  2,  2,  0,  0,  0,  0,  4,  4,  4,  4,  0,  0,  0,  0,  2,  2,  2,  2,  0,  0,  0,  0,
		10, 10, 10, 10,  0,  0,  0,  0,  2,  2,  2,  2,  0,  0,  0,  0,  4,  4,  4,  4,  0,  0,  0,  0,  2,  2,  2,  2,  0,  0,  0,  0,
		 6,  6,  6,  6,  0,  0,  0,  0,  2,  2,  2,  2,  0,  0,  0,  0,  4,  4,  4,  4,  0,  0,  0,  0,  2,  2,  2,  2,  0,  0,  0,  0,
		 8,  8,  8,  8,  0,  0,  0,  0,  2,  2,  2,  2,  0,  0,  0,  0,  4,  4,  4,  4,  0,  0,  0,  0,  2,  2,  2,  2,  0,  0,  0,  0,
		 6,  6,  6,  6,  0,  0,  0,  0,  2,  2,  2,  2,  0,  0,  0,  0,  4,  4,  4,  4,  0,  0,  0,  0,  2,  2,  2,  2,  0,  0,  0,  0,
	},
	{
		 0,  2,  0,  0,  0,  2,  0,  0,  0,  2,  0,  0,  0,  2,  0,  0,  2,  4,  2,  2,  2,  4,  2,  2,  0,  2,  0,  0,  0,  2,  0,  0,
		 4,  6,  4,  4,  4,  6,  4,  4,  0,  2,  0,  0,  0,  2,  0,  0,  2,  4,  2,  2,  2,  4,  2,  2,  0,  2,  0,  0,  0,  2,  0,  0,
		 6,  8,  6,  6,  6,  8,  6,  6,  0,  2,  0,  0,  0,  2,  0,  0,  2,  4,  2,  2,  2,  4,  2,  2,  0,  2,  0,  0,  0,  2,  0,  0,
		 4,  6,  4,  4,  4,  6,  4,  4,  0,  2,  0,  0,  0,  2,  0,  0,  2,  4,  2,  2,  2,  4,  2,  2,  0,  2,  0,  0,  0,  2,  0,  0,
		 8, 10,  8,  8,  8, 10,  8,  8,  0,  2,  0,  0,  0,  2,  0,  0,  2,  4,  2,  2,  2,  4,  2,  2,  0,  2,  0,  0,  0,  2,  0,  0,
		 4,  6,  4,  4,  4,  6,  4,  4,  0,  2,  0,  0,  0,  2,  0,  0,  2,  4,  2,  2,  2,  4,  2,  2,  0,  2,  0,  0,  0,  2,  0,  0,
		 6,  8,  6,  6,  6,  8,  6,  6,  0,  2,  0,  0,  0,  2,  0,  0,  2,  4,  2,  2,  2,  4,  2,  2,  0,  2,  0,  0,  0,  2,  0,  0,
		 4,  6,  4,  4,  4,  6,  4,  4,  0,  2,  0,  0,  0,  2,  0,  0,  2,  4,  2,  2,  2,  4,  2,  2,  0,  2,  0,  0,  0,  2,  0,  0,
	},
	{
		 0,  4,  2,  2,  0,  0,  0,  0,  0,  4,  2,  2,  0,  0,  0,  0,  0,  4,  2,  2,  0,  0,  0,  0,  0,  4,  2,  2,  0,  0,  0,  0,
		 2,  6,  4,  4,  2,  2,  2,  2,  2,  6,  4,  4,  2,  2,  2,  2,  0,  4,  2,  2,  0,  0,  0,  0,  0,  4,  2,  2,  0,  0,  0,  0,
		 4,  8,  6,  6,  4,  4,  4,  4,  4,  8,  6,  6,  4,  4,  4,  4,  0,  4,  2,  2,  0,  0,  0,  0,  0,  4,  2,  2,  0,  0,  0,  0,
		 2,  6,  4,  4,  2,  2,  2,  2,  2,  6,  4,  4,  2,  2,  2,  2,  0,  4,  2,  2,  0,  0,  0,  0,  0,  4,  2,  2,  0,  0,  0,  0,
		 6, 10,  8,  8,  6,  6,  6,  6,  6, 10,  8,  8,  6,  6,  6,  6,  0,  4,  2,  2,  0,  0,  0,  0,  0,  4,  2,  2,  0,  0,  0,  0,
		 2,  6,  4,  4,  2,  2,  2,  2,  2,  6,  4,  4,  2,  2,  2,  2,  0,  4,  2,  2,  0,  0,  0,  0,  0,  4,  2,  2,  0,  0,  0,  0,
		 4,  8,  6,  6,  4,  4,  4,  4,  4,  8,  6,  6,  4,  4,  4,  4,  0,  4,  2,  2,  0,  0,  0,  0,  0,  4,  2,  2,  0,  0,  0,  0,
		 2,  6,  4,  4,  2,  2,  2,  2,  2,  6,  4,  4,  2,  2,  2,  2,  0,  4,  2,  2,  0,  0,  0,  0,  0,  4,  2,  2,  0,  0,  0,  0,
	},
	{
		 0,  6,  4,  4,  2,  2,  2,  2,  0,  0,  0,  0,  0,  0,  0,  0,  0,  6,  4,  4,  2,  2,  2,  2,  0,  0,  0,  0,  0,  0,  0,  0,
		 0,  6,  4,  4,  2,  2,  2,  2,  0,  0,  0,  0,  0,  0,  0,  0,  0,  6,  4,  4,  2,  2,  2,  2,  0,  0,  0,  0,  0,  0,  0,  0,
		 2,  8,  6,  6,  4,  4,  4,  4,  2,  2,  2,  2,  2,  2,  2,  2,  2,  8,  6,  6,  4,  4,  4,  4,  2,  2,  2,  2,  2,  2,  2,  2,
		 0,  6,  4,  4,  2,  2,  2,  2,  0,  0,  0,  0,  0,  0,  0,  0,  0,  6,  4,  4,  2,  2,  2,  2,  0,  0,  0,  0,  0,  0,  0,  0,
		 4, 10,  8,  8,  6,  6,  6,  6,  4,  4,  4,  4,  4,  4,  4,  4,  4, 10,  8,  8,  6,  6,  6,  6,  4,  4,  4,  4,  4,  4,  4,  4,
		 0,  6,  4,  4,  2,  2,  2,  2,  0,  0,  0,  0,  0,  0,  0,  0,  0,  6,  4,  4,  2,  2,  2,  2,  0,  0,  0,  0,  0,  0,  0,  0,
		 2,  8,  6,  6,  4,  4,  4,  4,  2,  2,  2,  2,  2,  2,  2,  2,  2,  8,  6,  6,  4,  4,  4,  4,  2,  2,  2,  2,  2,  2,  2,  2,
		 0,  6,  4,  4,  2,  2,  2,  2,  0,  0,  0,  0,  0,  0,  0,  0,  0,  6,  4,  4,  2,  2,  2,  2,  0,  0,  0,  0,  0,  0,  0,  0,
	},
	{
		 0,  8,  6,  6,  4,  4,  4,  4,  2,  2,  2,  2,  2,  2,  2,  2,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		 0,  8,  6,  6,  4,  4,  4,  4,  2,  2,  2,  2,  2,  2,  2,  2,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		 0,  8,  6,  6,  4,  4,  4,  4,  2,  2,  2,  2,  2,  2,  2,  2,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		 0,  8,  6,  6,  4,  4,  4,  4,  2,  2,  2,  2,  2,  2,  2,  2,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		 2, 10,  8,  8,  6,  6,  6,  6,  4,  4,  4,  4,  4,  4,  4,  4,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
		 2, 10,  8,  8,  6,  6,  6,  6,  4,  4,  4,  4,  4,  4,  4,  4,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
		 0,  8,  6,  6,  4,  4,  4,  4,  2,  2,  2,  2,  2,  2,  2,  2,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		 0,  8,  6,  6,  4,  4,  4,  4,  2,  2,  2,  2,  2,  2,  2,  2,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
	},
	{
		 0, 10,  8,  8,  6,  6,  6,  6,  4,  4,  4,  4,  4,  4,  4,  4,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
		 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		 0, 10,  8,  8,  6,  6,  6,  6,  4,  4,  4,  4,  4,  4,  4,  4,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
		 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		 0, 10,  8,  8,  6,  6,  6,  6,  4,  4,  4,  4,  4,  4,  4,  4,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
		 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		 0, 10,  8,  8,  6,  6,  6,  6,  4,  4,  4,  4,  4,  4,  4,  4,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
		 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
	},
	{
		 0, 12, 10, 10,  8,  8,  8,  8,  6,  6,  6,  6,  6,  6,  6,  6,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,
		 2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
		 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		 0, 12, 10, 10,  8,  8,  8,  8,  6,  6,  6,  6,  6,  6,  6,  6,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,
		 2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
		 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
		 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
	},
};

/* bit masks for diagonal/vertical/all lines */
static const unsigned long long mask_x[64][4] = {
	{ 0x0000000000000001ULL, 0x8040201008040201ULL, 0x0101010101010101ULL, 0x81412111090503ffULL },
	{ 0x0000000000000102ULL, 0x0080402010080402ULL, 0x0202020202020202ULL, 0x02824222120a07ffULL },
	{ 0x0000000000010204ULL, 0x0000804020100804ULL, 0x0404040404040404ULL, 0x0404844424150effULL },
	{ 0x0000000001020408ULL, 0x0000008040201008ULL, 0x0808080808080808ULL, 0x08080888492a1cffULL },
	{ 0x0000000102040810ULL, 0x0000000080402010ULL, 0x1010101010101010ULL, 0x10101011925438ffULL },
	{ 0x0000010204081020ULL, 0x0000000000804020ULL, 0x2020202020202020ULL, 0x2020212224a870ffULL },
	{ 0x0001020408102040ULL, 0x0000000000008040ULL, 0x4040404040404040ULL, 0x404142444850e0ffULL },
	{ 0x0102040810204080ULL, 0x0000000000000080ULL, 0x8080808080808080ULL, 0x8182848890a0c0ffULL },
	{ 0x0000000000000102ULL, 0x4020100804020104ULL, 0x0101010101010101ULL, 0x412111090503ff03ULL },
	{ 0x0000000000010204ULL, 0x8040201008040201ULL, 0x0202020202020202ULL, 0x824222120a07ff07ULL },
	{ 0x0000000001020408ULL, 0x0080402010080402ULL, 0x0404040404040404ULL, 0x04844424150eff0eULL },
	{ 0x0000000102040810ULL, 0x0000804020100804ULL, 0x0808080808080808ULL, 0x080888492a1cff1cULL },
	{ 0x0000010204081020ULL, 0x0000008040201008ULL, 0x1010101010101010ULL, 0x101011925438ff38ULL },
	{ 0x0001020408102040ULL, 0x0000000080402010ULL, 0x2020202020202020ULL, 0x20212224a870ff70ULL },
	{ 0x0102040810204080ULL, 0x0000000000804020ULL, 0x4040404040404040ULL, 0x4142444850e0ffe0ULL },
	{ 0x0204081020408001ULL, 0x0000000000008040ULL, 0x8080808080808080ULL, 0x82848890a0c0ffc0ULL },
	{ 0x0000000000010204ULL, 0x201008040201000aULL, 0x0101010101010101ULL, 0x2111090503ff0305ULL },
	{ 0x0000000001020408ULL, 0x4020100804020101ULL, 0x0202020202020202ULL, 0x4222120a07ff070aULL },
	{ 0x0000000102040810ULL, 0x8040201008040201ULL, 0x0404040404040404ULL, 0x844424150eff0e15ULL },
	{ 0x0000010204081020ULL, 0x0080402010080402ULL, 0x0808080808080808ULL, 0x0888492a1cff1c2aULL },
	{ 0x0001020408102040ULL, 0x0000804020100804ULL, 0x1010101010101010ULL, 0x1011925438ff3854ULL },
	{ 0x0102040810204080ULL, 0x0000008040201008ULL, 0x2020202020202020ULL, 0x212224a870ff70a8ULL },
	{ 0x0204081020408001ULL, 0x0000000080402010ULL, 0x4040404040404040ULL, 0x42444850e0ffe050ULL },
	{ 0x0408102040800003ULL, 0x0000000000804020ULL, 0x8080808080808080ULL, 0x848890a0c0ffc0a0ULL },
	{ 0x0000000001020408ULL, 0x1008040201000016ULL, 0x0101010101010101ULL, 0x11090503ff030509ULL },
	{ 0x0000000102040810ULL, 0x2010080402010005ULL, 0x0202020202020202ULL, 0x22120a07ff070a12ULL },
	{ 0x0000010204081020ULL, 0x4020100804020101ULL, 0x0404040404040404ULL, 0x4424150eff0e1524ULL },
	{ 0x0001020408102040ULL, 0x8040201008040201ULL, 0x0808080808080808ULL, 0x88492a1cff1c2a49ULL },
	{ 0x0102040810204080ULL, 0x0080402010080402ULL, 0x1010101010101010ULL, 0x11925438ff385492ULL },
	{ 0x0204081020408001ULL, 0x0000804020100804ULL, 0x2020202020202020ULL, 0x2224a870ff70a824ULL },
	{ 0x0408102040800003ULL, 0x0000008040201008ULL, 0x4040404040404040ULL, 0x444850e0ffe05048ULL },
	{ 0x0810204080000007ULL, 0x0000000080402010ULL, 0x8080808080808080ULL, 0x8890a0c0ffc0a090ULL },
	{ 0x0000000102040810ULL, 0x080402010000002eULL, 0x0101010101010101ULL, 0x090503ff03050911ULL },
	{ 0x0000010204081020ULL, 0x100804020100000dULL, 0x0202020202020202ULL, 0x120a07ff070a1222ULL },
	{ 0x0001020408102040ULL, 0x2010080402010003ULL, 0x0404040404040404ULL, 0x24150eff0e152444ULL },
	{ 0x0102040810204080ULL, 0x4020100804020101ULL, 0x0808080808080808ULL, 0x492a1cff1c2a4988ULL },
	{ 0x0204081020408002ULL, 0x8040201008040201ULL, 0x1010101010101010ULL, 0x925438ff38549211ULL },
	{ 0x0408102040800005ULL, 0x0080402010080402ULL, 0x2020202020202020ULL, 0x24a870ff70a82422ULL },
	{ 0x081020408000000bULL, 0x0000804020100804ULL, 0x4040404040404040ULL, 0x4850e0ffe0504844ULL },
	{ 0x1020408000000017ULL, 0x0000008040201008ULL, 0x8080808080808080ULL, 0x90a0c0ffc0a09088ULL },
	{ 0x0000010204081020ULL, 0x040201000000005eULL, 0x0101010101010101ULL, 0x0503ff0305091121ULL },
	{ 0x0001020408102040ULL, 0x080402010000001dULL, 0x0202020202020202ULL, 0x0a07ff070a122242ULL },
	{ 0x0102040810204080ULL, 0x100804020100000bULL, 0x0404040404040404ULL, 0x150eff0e15244484ULL },
	{ 0x0204081020408001ULL, 0x2010080402010003ULL, 0x0808080808080808ULL, 0x2a1cff1c2a498808ULL },
	{ 0x0408102040800003ULL, 0x4020100804020101ULL, 0x1010101010101010ULL, 0x5438ff3854921110ULL },
	{ 0x081020408000000eULL, 0x8040201008040201ULL, 0x2020202020202020ULL, 0xa870ff70a8242221ULL },
	{ 0x102040800000001dULL, 0x0080402010080402ULL, 0x4040404040404040ULL, 0x50e0ffe050484442ULL },
	{ 0x204080000000003bULL, 0x0000804020100804ULL, 0x8080808080808080ULL, 0xa0c0ffc0a0908884ULL },
	{ 0x0001020408102040ULL, 0x02010000000000beULL, 0x0101010101010101ULL, 0x03ff030509112141ULL },
	{ 0x0102040810204080ULL, 0x040201000000003dULL, 0x0202020202020202ULL, 0x07ff070a12224282ULL },
	{ 0x0204081020408001ULL, 0x080402010000001bULL, 0x0404040404040404ULL, 0x0eff0e1524448404ULL },
	{ 0x0408102040800003ULL, 0x1008040201000007ULL, 0x0808080808080808ULL, 0x1cff1c2a49880808ULL },
	{ 0x0810204080000007ULL, 0x2010080402010003ULL, 0x1010101010101010ULL, 0x38ff385492111010ULL },
	{ 0x102040800000000fULL, 0x4020100804020101ULL, 0x2020202020202020ULL, 0x70ff70a824222120ULL },
	{ 0x204080000000003eULL, 0x8040201008040201ULL, 0x4040404040404040ULL, 0xe0ffe05048444241ULL },
	{ 0x408000000000007dULL, 0x0080402010080402ULL, 0x8080808080808080ULL, 0xc0ffc0a090888482ULL },
	{ 0x0102040810204080ULL, 0x010000000000027eULL, 0x0101010101010101ULL, 0xff03050911214181ULL },
	{ 0x0204081020408001ULL, 0x020100000000007dULL, 0x0202020202020202ULL, 0xff070a1222428202ULL },
	{ 0x0408102040800003ULL, 0x040201000000003bULL, 0x0404040404040404ULL, 0xff0e152444840404ULL },
	{ 0x0810204080000007ULL, 0x0804020100000017ULL, 0x0808080808080808ULL, 0xff1c2a4988080808ULL },
	{ 0x102040800000000fULL, 0x1008040201000007ULL, 0x1010101010101010ULL, 0xff38549211101010ULL },
	{ 0x204080000000001fULL, 0x2010080402010003ULL, 0x2020202020202020ULL, 0xff70a82422212020ULL },
	{ 0x408000000000003fULL, 0x4020100804020101ULL, 0x4040404040404040ULL, 0xffe0504844424140ULL },
	{ 0x800000000000017eULL, 0x8040201008040201ULL, 0x8080808080808080ULL, 0xffc0a09088848281ULL }
};

/**
 * Count last flipped discs when playing on the last empty.
 *
 * @param pos the last empty square.
 * @param P player's disc pattern.
 * @return flipped disc count.
 */

int last_flip(int pos, unsigned long long P)
{
	unsigned char	n_flipped;
	int	x = pos & 7;
	int	y = pos >> 3;

	P &= mask_x[pos][3];	// mask out unrelated bits to make dummy 0 bits for outside
	// n_flipped  = COUNT_FLIP[x][_bextr_u64(P, pos & 0x38, 8)];
	n_flipped  = COUNT_FLIP[x][(unsigned char) (P >> (pos & 0x38))];
	n_flipped += COUNT_FLIP[y][_pext_u64(P, mask_x[pos][0])];
	n_flipped += COUNT_FLIP[y][_pext_u64(P, mask_x[pos][1])];
	n_flipped += COUNT_FLIP[y][_pext_u64(P, mask_x[pos][2])];

	return n_flipped;
}
